# vim: ts=4:sw=4:syntax=perl

use gri_secmgr qw(get_user_name);
use gri_database;

sub mm_send_email4 {
my ($section,$user)=@_;

	my $s2=hex_to_string($section);
	my $u2=hex_to_string($user);

	my $db_dir=$::GRI_FRONTEND->get_global_directory("database");
	my $docrepos=$::GRI_FRONTEND->get_global_directory("docrepos");
	my $DB=new gri_database("$db_dir/gri.db");

	# We need to work out which reader groups the user belongs to
	# to work out which might be added, or issue an indication that
	# they do not belong to any groups as yet.

	my $username=get_user_name();
	my ($a,$b)=$DB->get_user_groups(user=>$username);

	$email=$::GRI_FRONTEND->get_user_email_address($v);
	if(!defined($email) || !length($email)) {
		return html_error("Failed to send message - unable to resolve name '$v' to email address.",0);
	}
	my $mail_to=$::GRI_FRONTEND->get_email_address("access_requests");
	if(!$mail_to) {
		return html_error("Failed to send message - unable to get access requests address.",0);
	}
	my $from=$::GRI_FRONTEND->get_email_sent_from_address();
	if(@$b==0) {
		# No reader sections for the user, so issue generic 
		# message ...
		my $subject="Request access: $u2 // $s2";
		my $cc="<HTML><BODY BGCOLOR=\"white\"><font style=\"font-family:verdana\" font-size=\"11\">User '$email' has requested access to document section '$s2'.\n";
		$cc.="\nUnfortunately this user does not yet belong to any configured document groups.\n</body></html>";
		my ($rc,$err)=$::GRI_FRONTEND->send_email({html=>1,subject=>$subject,to=>$mail_to,from=>$from,content=>$cc});
		if($rc) {
			return "A request for access to role '$r2' for user ID '$v' [$email] been successfully send to '$mail_to'.";
		} else {
			return html_error($err,0);
		}
		return "A request for access to document section '$s2' for user ID '$username' [$email] been successfully send to '$mail_to'.";
	} else {
		if($email && $mail_to) {
			my $cc="<HTML><BODY BGCOLOR=\"white\"><font style=\"font-family:verdana\" font-size=\"11\">The above user already has access to the following document groups - none of which are\n";
			$cc.="currently configured to read the document section '$s2'. You can use any of the\n";
			$cc.="following links to grant access to the group in question, or login to the GRI intranet.\n<P>";
			for my $cgroup (@$b) {
				my ($c1,$c2);
				($c1,$c2)=$DB->get_group(group => $cgroup);
				if($c1==1) {
					my $c3=string_to_hex($cgroup);
					my $l=link_to_str("stdlink","Read Only","grant_document_section_access","section=$section,user=$user,group=$c3,type=read");
					my $l2=link_to_str("stdlink","Publish","grant_document_section_access","section=$section,user=$user,group=$c3,type=publish");
					$cc.="Grant access to group '$c2->{description}' ::f$l $l2\n<br>";
				}
			}
			$cc.="</BODY></HTML>\n";
			my ($rc,$err)=$::GRI_FRONTEND->send_email({html=>1,subject=>$subject,to=>$mail_to,from=>$from,content=>$cc});
			if($rc) {
				return "A request for access to document section '$s2' for user ID '$username' [$email] been successfully send to '$mail_to'.";
			} else {
				return html_error($err,0);
			}
		}
	}
	if(! $email) {
		return "Failed to send message - unable to resolve name '$username' to email address.";
	}
	return "Failed to send message - unable to get configuration email address for 'access_requests'.";
}

1;

